package org.springblade.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.app.entity.Coupons;
import org.springblade.modules.app.entity.CouponsConfig;
import org.springblade.modules.app.mapper.CouponsMapper;
import org.springblade.modules.app.service.ICouponsConfigService;
import org.springblade.modules.app.service.ICouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * 优惠券 服务实现类
 *
 * @author BladeX
 * @since 2020-10-11
 */
@Service
public class CouponsServiceImpl extends ServiceImpl<CouponsMapper, Coupons> implements ICouponsService {

    @Autowired
    private ICouponsConfigService couponsConfigService;

    @Override
    public void registerUserScoupons(Long userid) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, 1);
        CouponsConfig configServiceOne = couponsConfigService.getOne(new LambdaQueryWrapper<CouponsConfig>().eq(CouponsConfig::getTypecode, "1").eq(CouponsConfig::getCouponsswitch, "1"));
        if (!StringUtils.isEmpty(configServiceOne)) {
            Coupons coupons = new Coupons();
            coupons.setId(IdWorker.getId());
//            coupons.setExpiredate(calendar.getTime());
            coupons.setUserid(userid);
            coupons.setDiscountprice(configServiceOne.getCouponsmoney());
            coupons.setTitle(configServiceOne.getTypename());
            coupons.setCtime(new Date());
            baseMapper.insert(coupons);
        }
    }
}
